/**************************** (C) COPYRIGHT 2017 Fortiortech shenzhen *****************************
* File Name          : Parameter.h
* Author             : Fortiortech  Appliction Team
* Version            : V1.0
* Date               : 2017-12-20
* Description        : This file contains all FOC debug parameter.
***************************************************************************************************
* All Rights Reserved
**************************************************************************************************/

/* Define to prevent recursive inclusion --------------------------------------------------------*/

#ifndef __Parameter_H_
#define __Parameter_H_

/* Define to prevent recursive inclusion -------------------------------------*/
/* Q format define ---------------------------------------------------------------------------------*/
#define _Q7(A)                          (int8)(A * 128)
#define _Q10(A)                         (int16)(A * 1024)                                             // Q15 format
#define _Q11(A)                         (int16)(A * 2048)                                                 // Q12 format
#define _Q12(A)                         (int16)(A * 4096)                                                 // Q12 format
#define _Q13(A)                         (int16)(A * 8192)                                                 // Q12 format
#define _Q15(A)                         (int16)(A * 32767)                                                // Q15 format
#define _2PI                            (3.1415926 * 2)                                                 // 2 PI value
#define _Q16                            (65535.0)                                                        // Q16 format value
/* Private define ------------------------------------------------------------*/
#define SystemPowerUpTime               (10000)                                                    // 上电等待时间，cpu计数时钟

/*芯片参数值------------------------------------------------------------------*/
/*CPU and PWM Parameter*/
#define SAMP_FREQ                       (PWM_FREQUENCY * 1000)                        // 采样频率(HZ)
#define SAMP_FREQ_BLDC                  (PWM_FREQUENCY_BLDC * 1000)                        // 采样频率(HZ)

#define TPWM_VALUE                      (1.0 / SAMP_FREQ)                             // 载波周期(S)
#define PWM_VALUE_LOAD_BLDC             (uint16)((MCU_CLOCK * 500 / PWM_FREQUENCY_BLDC)-1)   // PWM 定时器重载值



#define PWM_FREQUENCY_BLDC_count        (uint8)(PWM_FREQUENCY_BLDC)                                  // (kHz) 载波频率
#define PWM_FREQUENCY_BLDCHalf_count    (uint8)(PWM_FREQUENCY_BLDC_count>>1)                                  // (kHz) 载波频率

/*deadtime Parameter*/
#define PWM_LOAD_DEADTIME               ((PWM_DEADTIME*1000.0/41.67)-1)                          // 死区设置值
    
 #define HALLADVANCEEANGLE_CW_Value     (float)((60.0+HALLADVANCEEANGLE_CW)/60.0) 
 #define HALLADVANCEEANGLE_CCW_Value    (float)((60.0+HALLADVANCEEANGLE_CCW)/60.0)

 /* current set value */
#define I_ValueX(Curr_Value)            (Curr_Value * HW_RSHUNT * HW_AMPGAIN / (HW_ADC_REF))
#define I_Value(Curr_Value)             _Q15(I_ValueX(Curr_Value))

/*硬件板子参数设置值------------------------------------------------------------------*/
/*hardware current sample Parameter*/
/*电流基准的电路参数*/
#define HW_BOARD_CURR_MAX               (HW_ADC_REF / 2 / HW_AMPGAIN / HW_RSHUNT)         // 最大采样电流,2.702A
#define HW_BOARD_CURR_MIN               (-HW_BOARD_CURR_MAX)                                        // 最小采样电流,-2.702A
#define HW_BOARD_CURR_BASE              (HW_BOARD_CURR_MAX * 2)                                       // 电流基准//5.4A

/*hardware voltage sample Parameter*/
/*母线电压采样分压电路参数*/
#define HW_BOARD_VOLT_MAX               (HW_ADC_REF * RV)                       // (V)  ADC可测得的最大母线电压
#define HW_BOARD_VOLTAGE_BASE           (HW_BOARD_VOLT_MAX / 1.732)                              // 电压基准
#define HW_BOARD_VOLTAGE_VC             ((RV1 + RV2 + RV3 * VOLT_COMP) / (RV3 * VOLT_COMP))
#define HW_BOARD_VOLTAGE_BASE_Start     (HW_ADC_REF * HW_BOARD_VOLTAGE_VC / 1.732)               // 电压基准


/*硬件过流保护DAC值*/
#define DAC_OvercurrentValue            _Q7(I_ValueX(OverHardcurrentValue*2.0))+0x7F

#define Align_Theta                     (uint16)((float)(Align_Angle*182.038)) 

#define AL_THT(ALIGN_Temp)              (uint16)((float)(ALIGN_Temp*182.038))

#define BASE_FREQ                       ((MOTOR_SPEED_BASE / 60) * Pole_Pairs)  // 基准频率 

/*保护参数值------------------------------------------------------------------*/
/* protect value */
#define OVER_PROTECT_VALUE              _Q15(Over_Protect_Voltage  / HW_BOARD_VOLT_MAX)
#define UNDER_PROTECT_VALUE             _Q15(Under_Protect_Voltage / HW_BOARD_VOLT_MAX)
#define OVER_RECOVER_VALUE              _Q15(Over_Recover_Vlotage  / HW_BOARD_VOLT_MAX)
#define UNDER_RECOVER_VALUE             _Q15(Under_Recover_Vlotage / HW_BOARD_VOLT_MAX)



#define SPEED_K                         _Q15((float)(Motor_Max_Speed-Motor_Min_Speed)/(float)(MAX_Duty-MIN_Duty))



#define MIN_BLDC_Duty                   (uint16)(Min_BLDC_Duty*PWM_VALUE_LOAD_BLDC)  
#define MAX_BLDC_Duty                   (uint16)(Max_BLDC_Duty*PWM_VALUE_LOAD_BLDC) 


#define ON_duty                         (uint16)(ON_Duty*PWM_VALUE_LOAD_BLDC)  
#define OFF_duty                        (uint16)(OFF_Duty*PWM_VALUE_LOAD_BLDC) 


#define RAMP_target_STA                   (uint16)(RAMP_DUTY_STA*PWM_VALUE_LOAD_BLDC) 
#define RAMP_target_END                   (uint16)(RAMP_DUTY_END*PWM_VALUE_LOAD_BLDC) 
#define RAMP_target_INC                   (uint16)(RAMP_DUTY_INC*PWM_VALUE_LOAD_BLDC) 

#define BLDC_K                         ((float)(MAX_BLDC_Duty-MIN_BLDC_Duty)/(float)(MAX_Duty-MIN_Duty))


#define LEDVlotage_Value1                _Q15(Vlotage_Value1 /HW_BOARD_VOLT_MAX)
#define LEDVlotage_Value2                _Q15(Vlotage_Value2 /HW_BOARD_VOLT_MAX)
#define LEDVlotage_Value3                _Q15(Vlotage_Value3 /HW_BOARD_VOLT_MAX)



/*Current Calib:enable or disable*/
#define Calib_Time                     (1000)                                  // 校正次数，固定1000次，单位:次

#define ANGLE_MASK(X)                  {TIM1_CR1  =(TIM1_CR1 &(0x80)) | (X*127/60);}  
#define ANGLE_DELAY(X)                 {TIM1_CR2  =(TIM1_CR2 &(0x80)) | (X*127/60);}  

#define ActiveLowEnable                 (0x0540)                                //低电平有效
#define ActiveHighEnable                (0)                                     //高电平有效


	#if (PWM_Level_Mode == All_High_Level)
	
   #define H_MOSActive                     (ActiveHighEnable)                     //上桥预驱电平
   #define L_MOSActive                     (ActiveHighEnable)                     //小桥预驱电平 
	
	#elif (PWM_Level_Mode == All_Low_Level)
	
   #define H_MOSActive                     (ActiveLowEnable)                     //上桥预驱电平
   #define L_MOSActive                     (ActiveLowEnable)                     //小桥预驱电平 
	
	#elif (PWM_Level_Mode == UP_H_DOWN_L)
	
   #define H_MOSActive                     (ActiveHighEnable)                     //上桥预驱电平
   #define L_MOSActive                     (ActiveLowEnable)                     //小桥预驱电平 
	
	#elif (PWM_Level_Mode == UP_L_DOWN_H)
	
   #define H_MOSActive                     (ActiveLowEnable)                     //上桥预驱电平
   #define L_MOSActive                     (ActiveHighEnable)                     //小桥预驱电平 
	
	#endif 

#define PWM_BITCON                      (L_MOSActive+H_MOSActive*2)            //000高有效 AAA低有效  222上桥低  888下桥低

#define TUVW_OFF     0x0540
#define TULVLWL_ON   0x0540

#define TUH_VL4       0x0500
#define TUH_WL4       0x0500
#define TVH_WL4       0x0440
#define TVH_UL4       0x0440
#define TWH_UL4       0x0140
#define TWH_VL4       0x0140

/*****HPWM+LPWM*********/
#define TUH_VL0       0x0006
#define TUH_WL0       0x0012
#define TVH_WL0       0x0018
#define TVH_UL0       0x0009
#define TWH_UL0       0x0021
#define TWH_VL0       0x0024

/*****HPWM+LON*********/
#define TUH_VL1                         0x0102
#define TUH_WL1                         0x0402
#define TVH_WL1                         0x0408
#define TVH_UL1                         0x0048
#define TWH_UL1                         0x0060
#define TWH_VL1                         0x0120

/*****HON_PWM+LPWM*********/
#define TUH_VL2                         0x0084
#define TUH_WL2                         0x0402
#define TVH_WL2                         0x0210
#define TVH_UL2                         0x0048
#define TWH_UL2                         0x0801
#define TWH_VL2                         0x0120

#define TUH_VL3                         0x0102
#define TUH_WL3                         0x0090
#define TVH_WL3                         0x0408
#define TVH_UL3                         0x0201
#define TWH_UL3                         0x0060
#define TWH_VL3                         0x0804

#define UVW_OFF                         (TUVW_OFF^PWM_BITCON) 
#define ULVLWL_ON                       (TULVLWL_ON^PWM_BITCON)

#define CMP_UVW_UPDOOFF                (0x7000+UVW_OFF)  
#define CMP_UVW_UPDO                   0x7000 
#define CMP_W_DO                       0x2000  
#define CMP_V_UP                       0x3000 
#define CMP_U_DO                       0x4000
#define CMP_W_UP                       0x5000 
#define CMP_V_DO                       0x6000 
#define CMP_U_UP                       0x1000 
#define CMP_UVW_NO                     0x0000 


/*****HPWM+LPWM*********/
#define UH_VLA0            (TUH_VL0^PWM_BITCON)
#define UH_WLA0            (TUH_WL0^PWM_BITCON)
#define VH_WLA0            (TVH_WL0^PWM_BITCON)
#define VH_ULA0            (TVH_UL0^PWM_BITCON)
#define WH_ULA0            (TWH_UL0^PWM_BITCON)
#define WH_VLA0            (TWH_VL0^PWM_BITCON)

/*****HPWM+LON*********/
#define UH_VLA1            (TUH_VL1^PWM_BITCON)
#define UH_WLA1            (TUH_WL1^PWM_BITCON)
#define VH_WLA1            (TVH_WL1^PWM_BITCON)
#define VH_ULA1            (TVH_UL1^PWM_BITCON)
#define WH_ULA1            (TWH_UL1^PWM_BITCON)
#define WH_VLA1            (TWH_VL1^PWM_BITCON)


#define UH_VLA2            (TUH_VL2^PWM_BITCON)
#define UH_WLA2            (TUH_WL2^PWM_BITCON)
#define VH_WLA2            (TVH_WL2^PWM_BITCON)
#define VH_ULA2            (TVH_UL2^PWM_BITCON)
#define WH_ULA2            (TWH_UL2^PWM_BITCON)
#define WH_VLA2            (TWH_VL2^PWM_BITCON)

#define UH_VLA3            (TUH_VL3^PWM_BITCON)
#define UH_WLA3            (TUH_WL3^PWM_BITCON)
#define VH_WLA3            (TVH_WL3^PWM_BITCON)
#define VH_ULA3            (TVH_UL3^PWM_BITCON)
#define WH_ULA3            (TWH_UL3^PWM_BITCON)
#define WH_VLA3            (TWH_VL3^PWM_BITCON)


#define UH_VLA4            (TUH_VL4^PWM_BITCON)
#define UH_WLA4            (TUH_WL4^PWM_BITCON)
#define VH_WLA4            (TVH_WL4^PWM_BITCON)
#define VH_ULA4            (TVH_UL4^PWM_BITCON)
#define WH_ULA4            (TWH_UL4^PWM_BITCON)
#define WH_VLA4            (TWH_VL4^PWM_BITCON)

#define  Tim1_Ures_Down		              (0)														                                 
#define  Tim1_Ures_Up									  (1)														                                 

#define Motor_Bldc2Foc_Speed_BLDC       (uint16)(SAMP_FREQ_BLDC/(MOTOR_SPEED_BLDC2FOC_RPM/(10.0/Pole_Pairs)))

#define Motor_Foc2Bldc_TIM1_BCOR        (uint16)((MCU_CLOCK*5.0*1000000.0)/(Pole_Pairs*MOTOR_SPEED_BLDC2FOC_RPM)) 

/*注意溢出 预分频12M时，此时MOTOR_SPEED_FOC2BLDC_RPM最小为 = 12M/65535 =  183RPM */
#define Motor_Foc2Bldc_Speed_BLDC       (uint16)((MCU_CLOCK*5.0*1000000.0)/(Pole_Pairs*MOTOR_SPEED_FOC2BLDC_RPM)) 

#define Motor_Stall_Count              (uint16)(MOTOR_STALL_COUNT*PWM_FREQUENCY_BLDC)  


#define TempBEMFSpeedBase									(int32)(32767.0/8.0*(TIM2_Fre*60/Pole_Pairs/MOTOR_SPEED_BASE))
#define TempBEMFSpeedBase1								(int32)(32767.0/6.0/8.0*(TIM2_Fre*60/Pole_Pairs/MOTOR_SPEED_BASE))

#define Motor_Max_Speed                 _Q15(MOTOR_SPEED_MAX_RPM   / MOTOR_SPEED_BASE)
#define Motor_Min_Speed                 _Q15(MOTOR_SPEED_MIN_RPM   / MOTOR_SPEED_BASE)
#define Motor_default_Speed             _Q15(MOTOR_SPEED_DEFAULT_RPM   / MOTOR_SPEED_BASE)
#define Motor_MinCu_Speed               _Q15(MOTOR_SPEED_MIN_CU_RPM   / MOTOR_SPEED_BASE)

#define Motor_Max_Cust_Speed            _Q15(MOTOR_SPEED_MAX_CUST_RPM   / MOTOR_SPEED_BASE)






/******速度计算*******/
#define TIM1_PSC_FREQ           (6000000)
#define SPEEDSCALER             Q15(((60*(float)TIM1_PSC_FREQ)/(6*(float)Pole_Pairs*MOTOR_SPEED_BASE)))

#endif
